<template>
	<view class="bazaar">
		<view class="getH">
			<u-navbar title="订单市场" bgColor="#F7F9F9" :placeholder="true">
				<view class="title-left" slot="left" @click="onJump('/subpackage/choosAddress/choosAddress')">
					<text>{{city}}</text>
					<image class="arrow-down" src="@/static/bazaar/arrow_down.png" mode=""></image>
				</view>
			</u-navbar>
		</view>
		<u-sticky bgColor="#F7F9F9">
			<view class="screen">
				<view class="left" @click="typeShow = true">
					<text>{{serviceValue || '状态'}}</text>
					<image class="arrow-down" src="@/static/bazaar/arrow_down.png" mode=""></image>
				</view>
				<view class="right">
					<view class="time" @click="timeSort">
						<text>时间</text>
						<image v-if="timeSortType == 'desc'" src="@/static/bazaar/sort_down.png" mode=""></image>
						<image v-if="timeSortType == 'asce'" src="@/static/bazaar/sort_up.png" mode=""></image>
					</view>
					<view class="distance" @click="distanceSort">
						<text>距离</text>
						<image v-if="distanceSortType == 'desc'" src="@/static/bazaar/sort_down.png" mode=""></image>
						<image v-if="distanceSortType == 'asce'" src="@/static/bazaar/sort_up.png" mode=""></image>
					</view>
				</view>
			</view>
		</u-sticky>
		<scroll-view class="order-list" scroll-y :style="'height:calc(100vh - '+ offsetTop +')'" @scrolltolower="scrolltolower" v-if="orderList.length > 0">
			<view class="order-item" v-for="(item,index) in orderList" :key="index"
				@click="onJump(`/subpackage/order/detail?id=${item.id}`)">
				<view class="title">
					<view class="name">
						<text>{{item.repair_name}}</text>
						<view class="distance">
							<image class="distance-bg" src="@/static/bazaar/distance_bg.png" mode=""></image>
							<view class="number">
								<image class="distance-icon" src="@/static/bazaar/distance_icon.png" mode="">
								</image>
								<text>{{item.distance}}km</text>
							</view>
							<view class="tips" v-if="distanceSortType == 'desc' && index == 0">
								距离最近
							</view>
						</view>
					</view>
					<dwu-state :title="item.service_name"></dwu-state>
				</view>
				<view class="info-item">
					<view class="label">
						订单编号
					</view>
					<view class="value">
						{{item.order_sn}}
					</view>
				</view>
				<view class="info-item">
					<view class="label">
						下单时间
					</view>
					<view class="value">
						{{item.createtime_text || '-'}}
					</view>
				</view>
				<view class="info-item">
					<view class="label">
						期望上门时间
					</view>
					<view class="value">
						{{item.call_time_text}}
					</view>
				</view>
				<view class="info-item" v-if="item.is_phone == 1">
					<view class="label">
						项目地点
					</view>
					<view class="value">
						{{item.address || '-' }}
					</view>
				</view>
				<view class="btns">
					<view class="tel" @click.stop="callPhone(item)">
						<image src="@/static/bazaar/tel_icon.png" mode=""></image>
					</view>
					<view class="receive" @click.stop="receiving(item)" v-if="item.is_status == 0">
						接单
					</view>
				</view>
			</view>
		</scroll-view>
		<dwu-empty image="/static/public/empty_icon1.png" imageW="234rpx" imageH="166rpx" tips="暂无市场订单~"
			v-else></dwu-empty>
		<!-- 状态 -->
		<u-picker :show="typeShow" :columns="typeList" @cancel="typeShow = false" keyName="title"
			@confirm="chooseType"></u-picker>
		<!-- 弹窗 -->
		<dwu-modal ref="dwuModal"></dwu-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				offsetTop: 0,
				show: false,
				page: 1,
				pageSize: 10,
				flag: false,
				orderList: [],
				city: '',
				latitude: 0,
				longitude: 0,
				timeSortType: 'desc',
				distanceSortType: 'desc',
				serviceType: '',
				serviceValue: '',
				typeShow: false,
				typeList: [],
				integral: 0,
				offsetTop: 0,
				isSortType: 0,
			};
		},
		watch: {
			city() {
				let dwuLocationNow = uni.getStorageSync('dwu-location-now')
				this.city = dwuLocationNow.city
				this.latitude = dwuLocationNow.latitude
				this.longitude = dwuLocationNow.longitude
				this.getOrderList()
			}
		},
		onShow() {
			this.page = 1
			this.flag = false
			this.getOrderList()
			this.getHeight()
			this.show = true
		},
		onLoad() {
			let dwuLocationNow = uni.getStorageSync('dwu-location-now')
			this.city = dwuLocationNow.city
			this.latitude = dwuLocationNow.latitude
			this.longitude = dwuLocationNow.longitude
			this.getServiceType()
			this.getOrderList()
			this.phonemoney()
		},
		onReachBottom() {
			
		},
		methods: {
			getHeight() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.getH').boundingClientRect(data => {
					// if (data) {
					// 	this.offsetTop = data.height
					// 	this.$forceUpdate()
						
					// }
					if (data) {
						this.offsetTop = ((data.height * 2) + 100) + 'rpx'
						this.$forceUpdate()
					}
				}).exec();
			},
			scrolltolower() {
				if (this.flag) return
				this.page++
				this.flag = true
				this.getOrderList(true)
			},
			getServiceType() {
				this.request.http('/index/getServiceType', 'post').then(res => {
					let data = res.data
					if (data.code == 1) {
						this.typeList = [data.data]
					}
				})
			},
			phonemoney() {
				this.request.http('/index/phonemoney', 'post').then(res => {
					let data = res.data
					if (data.code == 1) {
						this.integral = data.data
					}
				})
			},
			chooseType(e) {
				this.serviceType = e.value[0].id
				this.serviceValue = e.value[0].title
				this.typeShow = false
				this.page = 1
				this.flag = false
				this.getOrderList()
			},
			timeSort() {
				console.log(2222)
				this.isSortType = 1
				this.distanceSortType = 'desc';
				this.timeSortType = this.timeSortType == 'desc' ? 'asce' : 'desc'
				this.page = 1
				this.flag = false
				this.getOrderList()
			},
			distanceSort() {
				this.isSortType = 0
				this.distanceSortType = this.distanceSortType == 'desc' ? 'asce' : 'desc'
				this.page = 1
				this.flag = false
				this.getOrderList()
			},
			getOrderList(next) {
				this.request.http('/order/index', 'post', {
					page: this.page,
					limit: this.pageSize,
					lat: this.latitude,
					lng: this.longitude,
					istype: this.isSortType,
					ordertime: this.timeSortType == 'desc' ? 1 : 2,
					orderdistance: this.distanceSortType == 'desc' ? 1 : 2,
					service_id: this.serviceType
				}).then(res => {
					let data = res.data
					if (data.code == 1) {
						if (next) {
							if (data.data.data.length > 0) {
								this.orderList = this.orderList.concat(data.data.data)
								this.flag = false
							}
						} else {
							this.orderList = data.data.data
							this.flag = false
						}
					}
				})
			},
			callPhone(item) {
				if (item.is_phone == 0) {
					this.$refs.dwuModal.open({
						content: `请支付${this.integral}积分获取发单人联系电话。`,
						success: (res) => {
							this.request.http('/sfuser/getuserphone', 'post', {
								order_id: item.id
							}).then(res => {
								let data = res.data
								if (data.code == 1) {
									item.is_phone = 1
									this.$refs.dwuModal.open({
										content: '积分支付成功，本次订单将会为您锁定10分钟，请尽快与发单人取得联系，完成接单，发单人电话 ：' +
											data.data,
										submitText: '复制电话',
										success: (res) => {
											uni.setClipboardData({
												data: data.data,
												success: (res) => {
													uni.showToast({
														title: '电话已复制'
													})
												}
											})
										}
									})
								}
							})
						}
					})
				} else {
					uni.makePhoneCall({
						phoneNumber: item.phone
					})
				}


			},
			receiving(item) {
				if (item.is_phone == 0) {
					this.$refs.dwuModal.open({
						content: `接单前须先支付${this.integral}积分获取发单人联系电话，进行电话沟通确认! 点击确定将获取联系人电话并扣除相应积分。`,
						success: (res) => {
							this.request.http('/sfuser/getuserphone', 'post', {
								order_id: item.id
							}).then(res => {
								let data = res.data
								if (data.code == 1) {
									item.is_phone = 1
									this.$refs.dwuModal.open({
										content: '积分支付成功，本次订单将会为您锁定10分钟，请尽快与发单人取得联系，完成接单，发单人电话 ：' +
											data.data,
										submitText: '复制电话',
										success: (res) => {
											uni.setClipboardData({
												data: phone,
												success: (res) => {
													uni.showToast({
														title: '电话已复制'
													})
												}
											})
										}
									})
								}
							})
						}
					})
				} else {
					this.request.http('/sfuser/orderTaking', 'post', {
						order_id: item.id
					}).then(res => {
						let data = res.data
						if (data.code == 1) {
							uni.showToast({
								title: '接单成功'
							})
						}
					})
				}
			},
			onJump(url) {
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>

<style lang="less">
	page {
		background-color: #F7F9F9;
	}

	.bazaar {
		.title-left {
			display: flex;
			align-items: center;

			text {
				font-weight: 500;
				font-size: 23rpx;
				color: #5B5B5B;
				line-height: 33rpx;
			}

			.arrow-down {
				margin-left: 4rpx;
				width: 12rpx;
				height: 10rpx;
			}
		}

		.screen {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 26rpx 62rpx 26rpx 54rpx;

			.left {
				display: flex;
				align-items: center;

				text {
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 23rpx;
					color: #666666;
					line-height: 33rpx;
				}

				.arrow-down {
					margin-left: 4rpx;
					width: 12rpx;
					height: 10rpx;
				}
			}

			.right {
				display: flex;
				align-items: center;

				.time,
				.distance {
					display: flex;
					align-items: center;

					text {
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 23rpx;
						color: #666666;
						line-height: 33rpx;
					}

					image {
						margin-left: 4rpx;
						width: 14rpx;
						height: 20rpx;
					}
				}

				.distance {
					margin-left: 32rpx;
				}
			}
		}

		.order-list {
			padding: 22rpx 26rpx;

			.order-item {
				margin-bottom: 30rpx;
				padding: 28rpx 30rpx;
				background: #FFFFFF;
				box-shadow: 0rpx 4rpx 13rpx 8rpx rgba(245, 245, 245, 0.16);
				border-radius: 35rpx;

				.title {
					margin-bottom: 28rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.name {
						display: flex;
						align-items: center;

						&>text {
							font-family: PingFangSC, PingFang SC;
							font-weight: 600;
							font-size: 27rpx;
							color: #474747;
						}

						.distance {
							margin-left: 12rpx;
							position: relative;
							// width: 283rpx;
							padding: 0 25rpx;
							height: 45rpx;
							display: flex;
							align-items: center;
							justify-content: center;

							.distance-bg {
								position: absolute;
								z-index: 1;
								top: 0;
								left: 0;
								width: 100%;
								height: 100%;
							}

							.number {
								position: relative;
								z-index: 10;
								display: flex;
								align-items: center;

								.distance-icon {
									margin-right: 6rpx;
									width: 21rpx;
									height: 21rpx;
								}

								text {
									font-family: PingFangSC, PingFang SC;
									font-weight: 500;
									font-size: 21rpx;
									color: #999999;
								}
							}

							.tips {
								position: relative;
								z-index: 10;
								padding-left: 10rpx;
								margin-left: 32rpx;
								border-left: 2rpx solid #C6C6C6;
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 21rpx;
								color: #A0A0A0;
							}
						}
					}
				}

				.info-item {
					margin-bottom: 22rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.label {
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 23rpx;
						color: #666666;
						line-height: 33rpx;
					}

					.value {
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 23rpx;
						color: #868686;
						line-height: 33rpx;
					}
				}

				.btns {
					display: flex;
					align-items: center;
					justify-content: flex-end;

					.tel {
						display: flex;
						align-items: center;
						justify-content: center;
						width: 155rpx;
						height: 69rpx;
						background: #FFFFFF;
						border-radius: 23rpx;
						border: 2rpx solid #ECECEC;

						image {
							width: 31rpx;
							height: 31rpx;
						}
					}

					.receive {
						margin-left: 34rpx;
						width: 155rpx;
						height: 69rpx;
						background: #3D3D3D;
						border-radius: 23rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 27rpx;
						color: #B0DD67;
						line-height: 69rpx;
						text-align: center;
					}
				}
			}
		}
	}
</style>